package com.sxfq.loansupermarket.backsystem.modular.basicData.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.support.DateTimeKit;
import com.sxfq.loansupermarket.backsystem.core.util.CommUtils;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.dao.VehicleDao;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchVehicleInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.VehicleInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.IVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class VehicleServiceImpl implements IVehicleService {

    @Autowired
    private VehicleDao dao;

    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<VehicleInfo> selectPageListByQueryGetMap(Page<VehicleInfo> page, SearchVehicleInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getVehicleName())) {
            params.put("vehicleName", searchInfo.getVehicleName());
        }
        return dao.getQueryPage(page, params);
    }

    public VehicleInfo selectOneById(String  vehicleId) {
        String sql = "select * from vehicle_info where id= '" + vehicleId + "'";
        VehicleInfo info = sqlMapper.selectOne(sql, VehicleInfo.class);
        return info;
    }

    public int insert(VehicleInfo info) {
        String sql = "INSERT INTO `vehicle_info` ( `vehicle_name`, `remarks`, `num`) VALUES" +
                "( '" + info.getVehicleName().trim() + "','" + info.getRemarks().trim() + "'," + info.getNum()+ ");";
        return sqlMapper.insert(sql);
    }


    @Transactional
    public void delete(String id) {
        String sql = "delete from vehicle_info  where id = '" + id + "'";
        sqlMapper.delete(sql);
    }

    public int update(VehicleInfo info) {
        return dao.updateById(info);
    }

    public List<Map<String,Object>>  list(){
        String sql = "select id, vehicle_name from vehicle_info order by  num asc;";
        return sqlMapper.selectList(sql);
    }
}
